#import "NSArrayCustomSort.h"

/**
 * NSArray foundation class extended with a CustomSort category.
 *
 * The NSArray foundation class is extended to support sorting an
 * array based on the context of another array.
 *
 * @author      James Lin
 * @version     0.6.2
 * @since       0.6.2
 * @see         NSArray
 */
@implementation NSArray (CustomSort)

/**
 * Compare two integers and return the descending order relation.
 */
NSInteger functionForContextSort(id obj1, id obj2, void *context) {
	NSUInteger value1 = (NSUInteger)obj1;
	NSUInteger value2 = (NSUInteger)obj2;
	
	if (value1 < value2) {
		return NSOrderedDescending;
	} else if (value1 > value2) {
		return NSOrderedAscending;
	} else {
		return NSOrderedSame;
	}
}

/**
 * Sort the array using the context of another array.
 *
 * @param array  the other array of which to use as the context
 * @return       sorted array using the context of the other array
 */
- (NSArray*)sortedArrayUsingOtherArray:(NSArray*)array {
	return [self sortedArrayUsingFunction:functionForContextSort context:array];
}

@end